﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System;
using System.Collections.Generic;
using DocumentFormat.OpenXml.Spreadsheet;
using ExtremeML.Core;
using ExtremeML.Spreadsheet.Address;

namespace ExtremeML.Spreadsheet
{
	// This is a partial class that adds custom functionality to its auto-generated partner.
	public sealed partial class ColumnWrapper
	{
		#region Custom Properties

		/// <summary>
		/// Gets the containing <see cref="WorksheetWrapper"/>.
		/// </summary>
		public WorksheetWrapper Worksheet
		{
			get { return (WorksheetWrapper)Parent.Parent.Parent.Parent; }
		}

		/// <summary>
		/// Gets the <see cref="ColumnSpan"/> that manages range addressing for the wrapped column.
		/// </summary>
		public ColumnSpan ColumnSpan { get; private set; }

		#endregion
		#region Custom Methods


		#endregion
		#region Overrides

		/// <summary>
		/// Implements custom initialization functionality.
		/// </summary>
		protected override void OnCustomInitializing()
		{
			ColumnSpan = new ColumnSpan();
		}

		/// <summary>
		/// Implements custom population functionality.
		/// </summary>
		protected override void OnCustomDecomposing()
		{
			ColumnSpan = new ColumnSpan((int)Min - 1, (int)Max - 1);
		}

		/// <summary>
		/// Establishes relationships with other package content objects.
		/// </summary>
		protected override void OnCustomActivating()
		{
			RegisterAddressDependencies();
		}

		/// <summary>
		/// Implements custom persistence functionality.
		/// </summary>
		protected override void OnCustomRecomposing(
			Column wrapped)
		{
			Min = (uint)ColumnSpan.Begin + 1;
			Max = (uint)ColumnSpan.End + 1;
		}

		/// <summary>
		/// Implements custom deletion functionality.
		/// </summary>
		protected override void OnCustomRemoved()
		{
			ReleaseAddressDependencies();
		}

		/// <summary>
		/// Returns the set of <see cref="IWrapper"/> instances defined as custom properties.
		/// </summary>
		protected override IEnumerable<IWrapper> GetCustomWrapperMembers()
		{
			yield return ColumnSpan;
		}

		#endregion
		#region Implementation Methods

		/// <summary>
		/// Registers addressable contained objects for notification of address changes.
		/// </summary>
		private void RegisterAddressDependencies()
		{
			Worksheet.WorksheetPart.AddressDependencies.Add(ColumnSpan);

			ColumnSpan.SpanDeleted += HandleSpanDeletion;
		}

		/// <summary>
		/// Releases previously registered contained objects from notification of address changes.
		/// </summary>
		private void ReleaseAddressDependencies()
		{
			if (Worksheet.WorksheetPart.AddressDependencies.Contains(ColumnSpan))
			{
				Worksheet.WorksheetPart.AddressDependencies.Remove(ColumnSpan);
			}

			ColumnSpan.SpanDeleted -= HandleSpanDeletion;
		}

		/// <summary>
		/// Handles the span deletion event of <see cref="ColumnSpan"/>.
		/// </summary>
		private void HandleSpanDeletion(
			object sender,
			EventArgs e)
		{
			Remove();
		}

		#endregion
		#region Static Methods

		internal static double GetDefaultColumnWidth()
		{
//Hack: this is hard coded pending information necessary to calculate a value that varies by font.
			return 9.140625d;
		}

		#endregion
	}
}
